<?php

class image_process extends Admin_Controller
{
	function sortColumn()
	{
		global $CI;
		
		// model
		$CI->load->module_model('image.image_list_model');
		$col = $CI->uri->segment($CI->image_list_model->item_segment);
		
		$order = array(''=>'desc', 'desc'=>'asc', 'asc'=>'desc');
		foreach ($CI->image_list_model->columns as $k=>$v)
		{
			if (strcmp($k, $col) == 0)
			{
				$CI->image_list_model->cache->add($k, $order[$CI->image_list_model->cache->load($k)]);
			} else
				$CI->image_list_model->cache->add($k, '');
		}
		
		// go to original page
		redirect($CI->image_list_model->url_prefix.'/'.$CI->image_list_model->start);
	}
	
	function deleteAll()
	{
		global $CI;
		
		// model
		$CI->load->module_model('image.image_list_model');
		$CI->load->module_model('image.display_image_model');
		
		$flag = 0;
		if (isset($_POST['uid']) && is_array($_POST['uid']))
		{
			foreach ($_POST['uid'] as $id)
			{
				if ($CI->image_list_model->check_delete($id))
				{
					$flag = 1;
					$CI->display_image_model->delete_image($id);
					$CI->db->delete(IMAGE, array(IMAGE_IMAGE_ID=>$id, IMAGE_FRANCHISE_ID=>$CI->image_list_model->admin_id()));			
				}
			}
		}
		
		if ($flag)
			// message
			$CI->image_list_model->cache->add('msg', $CI->lang->line('list_delete_all'));
		else
			// message
			$CI->image_list_model->cache->add('msg', $CI->lang->line('list_cannot_delete')." : ".$CI->image_list_model->error_string);
		
		// go to original page
		redirect($CI->image_list_model->url_prefix.'/'.$CI->image_list_model->start);
	}
	
	function deleteOne()
	{
		global $CI;
		
		// model
		$CI->load->module_model('image.image_list_model');
		$CI->load->module_model('image.display_image_model');
		
		$id = $CI->uri->segment($CI->image_list_model->item_segment);
		
		$CI->db->delete(IMAGE, array(IMAGE_IMAGE_ID=>$id));		
		// go to original page
		redirect($CI->image_list_model->url_prefix.'/'.$CI->image_list_model->start);
	}	
	
	function saveItem()
	{
		global $CI;
		
		// model
		$CI->load->module_model('image.image_detail_model');
		/*
		// validate
		$CI->load->library('form_validation');
        $CI->form_validation->set_rules('imagepath', 'Image', 'trim|required');
        if ($CI->form_validation->run() == FALSE)
		{		

			// go to original page
			redirect($CI->image_detail_model->url_prefix.'/'.$CI->image_detail_model->start.'/'.$CI->image_detail_model->item);
		}
		*/
		// upload
		$file_name = '';
		if ($_FILES['imagepath']['tmp_name'])
		{
			$CI->load->library("fckeditor");
			$path = $CI->fckeditor->UploadPath.'/images';;			
			$path = rtrim($_SERVER['DOCUMENT_ROOT'], "/").$path;
			
			$config['upload_path'] = $path;
			$config['allowed_types'] = 'gif|jpg|jpeg|png';
			$config['max_width'] = '1024';
			$config['max_height'] = '768';			
			$config['max_size'] = '1024';
			$config['max_filename'] = '24';
			$CI->load->library('upload', $config);

			if ($CI->upload->do_upload('imagepath'))
			{
				// filename
				$file_name = $CI->upload->file_name;
				//$_POST['location'] = $file_name;			
				
			} else {
				$CI->image_detail_model->cache->add('msg', $CI->lang->line('detail_cannot_save')." : ".$CI->upload->display_errors());
				$CI->image_detail_model->cache->add('post', $_POST);
				
				// go to original page
				redirect($CI->image_detail_model->url_prefix.'/'.$CI->image_detail_model->start.'/'.$CI->image_detail_model->item);
			}
		}
		else{
			redirect($CI->image_detail_model->url_prefix2.'/'.$CI->image_detail_model->start);
		}

		// save to image
		if ($CI->image_detail_model->hasImage())
		{
			/*
			 select old name			*/
			$old_name = '';
			$q = $CI->db->get_where(IMAGE, array(IMAGE_IMAGE_ID=>$CI->image_detail_model->item));
			$row = $q->row();
			if($row){
				$old_name = $row->{IMAGE_NAME};
			}
			// update
			$sql = "UPDATE ".IMAGE." SET";
			$sql .= " ".IMAGE_NAME."=".$CI->db->escape($file_name);
			$sql .= " WHERE ".IMAGE_IMAGE_ID."=".$CI->image_detail_model->item;
			$CI->db->query($sql);
			
			if($old_name){
				@unlink($path.'/'.$old_name);
			}
			
		} else {
			$CI->image_detail_model->item = $CI->image_detail_model->maxkey();
			// insert
			$data = array(
				IMAGE_NAME=>$file_name
				//IMAGE_IMAGE_ID=>$CI->image_detail_model->item,
			);
			$CI->db->insert(IMAGE, $data);
		}
		
		// message
		$CI->image_detail_model->cache->add('msg', $CI->lang->line('detail_save'));
	
		// go to original page
		redirect($CI->image_detail_model->url_prefix2.'/'.$CI->image_detail_model->start);
	}		
}

?>